<?php
require_once 'start.php';

$request = new \lib\Request();

if($_POST['step'] == '2'){
    $request->url($_POST['url'])->autoCookie()->send();
    //获取vfwebqq
    $vf = $request
	    ->url('https://s.web2.qq.com/api/getvfwebqq?ptwebqq=&clientid=53999199&psessionid=&t=1539686933465')
	    ->setReferer('https://s.web2.qq.com/proxy.html?v=20130916001&callback=1&id=1')
	    ->autoCookie()
	    ->send();
    $vf = json_decode($vf, true);
    $response = $request
        ->url('https://d1.web2.qq.com/channel/login2')
        ->setData(['r'=>'{"ptwebqq":"","clientid":53999199,"psessionid":"","status":"online"}'])
        ->setReferer('https://d1.web2.qq.com/proxy.html?v=20151105001&callback=1&id=2')
        ->setMethod('post')
        ->autoCookie()
        ->send();
    $json = json_decode($response, true);
    if($json['retcode'] == 0){

	    $friends = $request
		    ->url('https://s.web2.qq.com/api/get_user_friends2')
		    ->setData(['r'=>'{"vfwebqq":"'.$vf['result']['vfwebqq'].'","hash":"'.\lib\Encoder::hash2($json['result']['uin'], '').'"}'])
		    ->setReferer('https://s.web2.qq.com/proxy.html?v=20130916001&callback=1&id=1')
		    ->setMethod('post')
		    ->autoCookie()
		    ->send();
        $friends = json_decode($friends, true);

	    $groups = $request
		    ->url('https://s.web2.qq.com/api/get_group_name_list_mask2')
		    ->setData(['r'=>'{"vfwebqq":"'.$vf['result']['vfwebqq'].'","hash":"'.\lib\Encoder::hash2($json['result']['uin'], '').'"}'])
		    ->setReferer('https://s.web2.qq.com/proxy.html?v=20130916001&callback=1&id=1')
		    ->setMethod('post')
		    ->autoCookie()
		    ->send();
        $groups = json_decode($groups, true);

	    $onlineUrl = 'https://d1.web2.qq.com/channel/get_online_buddies2?vfwebqq='.$vf['result']['vfwebqq'].'&clientid=53999199&psessionid='.$json['result']['psessionid'].'&t=' . \lib\Encoder::times();
        $online = $request
            ->url($onlineUrl)
            ->setReferer('https://d1.web2.qq.com/proxy.html?v=20151105001&callback=1&id=2')
            ->autoCookie()
            ->send();
        $online = json_decode($online, true);

        //请求recent(最近聊天列表)
	    $vfb = '{"vfwebqq":"'.$vf['result']['vfwebqq'].'","clientid":53999199,"psessionid":"'.$json['result']['psessionid'].'"}';
        $recent = $request
            ->url('https://d1.web2.qq.com/channel/get_recent_list2')
            ->setReferer('https://d1.web2.qq.com/proxy.html?v=20151105001&callback=1&id=2')
	        ->setData(['r'=>$vfb])
	        ->setMethod('post')
            ->autoCookie()
            ->send();
        $recent = json_decode($recent, true);

        \lib\Cache::set('conf', array(
            'vf'    => $vf['result'],
            'login2'    => $json['result'],
            'friends'   => $friends,
            'groups'    => $groups,
            'online'    => $online,
            'recent'    => $recent
        ));
      
        exec('php cli.php');
        exit;

//        if('WINNT' == PHP_OS){
//            $qq = new \lib\Qq();
//            $qq->start();
//        }else{
//            // > /dev/null &
//            exec('php cli.php');
//        }
    }else{
        echo $response;
    }
}else{
    $url = 'https://xui.ptlogin2.qq.com/cgi-bin/xlogin?daid=164&target=self&style=40&pt_disable_pwd=1&mibao_css=m_webqq&appid=501004106&enable_qlogin=0&no_verifyimg=1&s_url=https://web2.qq.com/proxy.html&f_url=loginerroralert&strong_login=1&login_state=10&t=20131024001';
    $request->url($url)->autoCookie()->send();
    echo 'ok';
}